package zh.dummy.netty.server.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import zh.dummy.netty.core.protocol.MessageType;
import zh.dummy.netty.core.protocol.NettyMessage;
import zh.dummy.netty.server.util.NettyMessageBuilder;

public class HeartBeatRespHandler extends ChannelHandlerAdapter {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	public void channelRead(ChannelHandlerContext ctx,Object msg){
		NettyMessage message = (NettyMessage) msg;
		//返回心跳应答消息
		if(message.getHeader() != null
				&& message.getHeader().getType() == MessageType.HEARTBEAT_REQ.value()){
			if(logger.isDebugEnabled()){
				logger.debug("reveived heartbeat request");
			}
			NettyMessage heartBeat = NettyMessageBuilder.buildHeatBeatResp();
			ctx.writeAndFlush(heartBeat);
			if(logger.isDebugEnabled()){
				logger.debug("reveived heartbeat response");
			}
		}else{
			ctx.fireChannelRead(msg);
		}
	}

}
